package cn.zn.modules.pz.his.service.impl;

import cn.zn.framework.common.vo.FinanceValues;
import cn.zn.modules.pz.his.entity.PzAttachment;
import cn.zn.modules.pz.his.entity.QPzAttachment;
import cn.zn.modules.pz.his.entity.vo.PzAttachmentVO;
import cn.zn.modules.pz.his.repository.PzAttachmentRepository;
import cn.zn.modules.pz.his.service.PzAttachmentService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
@Slf4j
public class PzAttachmentServiceImpl implements PzAttachmentService {

    @Autowired
    private PzAttachmentRepository pzAttachmentRepository;
    @Autowired
    FinanceValues financeValues;

    @Override
    public List<PzAttachmentVO> findByMainId(Integer ap, String mainId) {
        QPzAttachment qPzAttachment = QPzAttachment.pzAttachment;
        Iterator<PzAttachment> iterator = pzAttachmentRepository.findAll(qPzAttachment.ap.eq(ap).and(qPzAttachment.mainId.eq(mainId))).iterator();
        List<PzAttachmentVO> pzAttachmentVOS = Lists.newArrayList();
        PzAttachment pzAttachment;
        PzAttachmentVO pzAttachmentVO;
        while (iterator.hasNext()) {
            pzAttachment = iterator.next();
            pzAttachmentVO = new PzAttachmentVO(financeValues.getFile().getDomain());
            BeanUtils.copyProperties(pzAttachment, pzAttachmentVO);
            pzAttachmentVOS.add(pzAttachmentVO);
        }
        return pzAttachmentVOS;
    }
}
